package org.chen.system.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.chen.common.system.dto.AttractionDTO;
import org.chen.common.system.dto.CityAttractionDTO;
import org.chen.common.system.dto.ProvinceCapitalDto;
import org.chen.common.system.province.Attraction;
import org.chen.common.system.province.ProvinceCapital;
import org.chen.common.result.ResultVO;
import org.chen.system.service.AttractionService;
import org.chen.system.service.ProvinceCapitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cities")
@Tag(name = "不重要的接口", description = "不重要的接口")
public class ProvinceCapitalController {
    
    @Resource
    private ProvinceCapitalService provinceCapitalService;
    
    @Resource
    private AttractionService attractionService;
    
    /**
     * 获取所有省会城市及其景点
     */
    @GetMapping
    public ResultVO<List<CityAttractionDTO>> getAllCitiesWithAttractions() {
        List<ProvinceCapital> capitals = provinceCapitalService.list();
        List<CityAttractionDTO> collect = capitals.stream().map(capital -> {
            CityAttractionDTO dto = new CityAttractionDTO();
            BeanUtils.copyProperties(capital, dto);

            List<Attraction> attractions = attractionService.getAttractionsByCapitalId(capital.getId());
            List<AttractionDTO> attractionDTOs = attractions.stream().map(attraction -> {
                AttractionDTO attractionDTO = new AttractionDTO();
                BeanUtils.copyProperties(attraction, attractionDTO);
                return attractionDTO;
            }).collect(Collectors.toList());

            dto.setAttractions(attractionDTOs);
            return dto;
        }).collect(Collectors.toList());
        return ResultVO.success(collect);
    }
    
    /**
     * 根据名称查找对应的省会城市ID
     */
    @GetMapping("/search")
    public ResultVO<ProvinceCapital> searchCapitalId(@RequestParam String name) {
        // 先尝试作为省会城市名称查找
        ProvinceCapital capitalIdByName = provinceCapitalService.findCapitalIdByName(name);
        if (capitalIdByName != null) {
            return ResultVO.success(capitalIdByName);
        }
        
        // 再尝试作为景点或小城市名称查找
        return ResultVO.success(attractionService.findCapitalIdByAttractionName(name));
    }

    /**
     * 根据平台和ID查找对应的省会城市ID
     */
    @GetMapping("/search/platform")
    public ResultVO<ProvinceCapital> searchCapitalIdByPlatform(@RequestParam String platform, @RequestParam String platformId) {
        // 先尝试作为省会城市的平台ID查找
        ProvinceCapital capitalId = attractionService.findCapitalIdByPlatformId(platform, platformId);
        if (capitalId != null) {
            return ResultVO.success(capitalId);
        }

        // 再尝试作为景点或小城市的平台ID查找
        return ResultVO.success(attractionService.findCapitalIdByAttractionPlatformId(platform, platformId));
    }
    
    /**
     * 添加省会城市
     */
    @PostMapping("/capital")
    public ResultVO<Boolean> addCapital(@RequestBody ProvinceCapitalDto capital) {
        ProvinceCapital provinceCapital = new ProvinceCapital();
        provinceCapital.setName(capital.getName());
        provinceCapital.setProvince(capital.getProvince());
        provinceCapital.setCtripCityId(capital.getCtripId());
        provinceCapital.setTongchengCityId(capital.getTongchengId());
        provinceCapital.setMafengwoCityId(capital.getMafengwoId());
        return ResultVO.success(provinceCapitalService.save(provinceCapital));
    }

    /**
     * 更新省会城市
     */
    @PutMapping("/capital/{id}")
    public ResultVO<Object> updateCapital(@PathVariable Long id, @RequestBody ProvinceCapitalDto capital) {
        capital.setId(id);
        try {
            // 检查是否为系统默认省会城市
            return ResultVO.success(provinceCapitalService.updateProvinceById(capital));
        } catch (Exception e) {
            // 捕获异常并返回失败结果
            return ResultVO.fail("更新省会城市失败: " + e.getMessage());
        }
    }

    /**
     * 删除省会城市
     */
    @DeleteMapping("/capital/{id}")
    public ResultVO<Object> deleteCapital(@PathVariable Long id) {
        ProvinceCapital byId = provinceCapitalService.getById(id);
        if (byId.getIsSystem()){
            return ResultVO.fail("系统默认省会城市，无法删除");
        }

        // 检查是否有关联的景点或小城市
        List<Attraction> attractions = attractionService.getAttractionsByCapitalId(id);
        if (!attractions.isEmpty()) {
            return ResultVO.fail("该省会城市下还有" + attractions.size() + "个景点或小城市，无法删除");
        }

        return ResultVO.success(provinceCapitalService.removeById(id));
    }

    /**
     * 根据ID获取省会城市
     */
    @GetMapping("/capital/{id}")
    public ResultVO<ProvinceCapital> getCapitalById(@PathVariable Long id) {
        return ResultVO.success(provinceCapitalService.getById(id));
    }

    /**
     * 添加景点或小城市
     */
    @PostMapping("/attraction")
    public ResultVO<Boolean> addAttraction(@RequestBody Attraction attraction) {
        return ResultVO.success(attractionService.save(attraction));
    }

    /**
     * 更新景点或小城市
     */
    @PutMapping("/attraction/{id}")
    public ResultVO<Boolean> updateAttraction(@PathVariable Long id, @RequestBody Attraction attraction) {
        attraction.setId(id);
        return ResultVO.success(attractionService.updateById(attraction));
    }

    /**
     * 删除景点或小城市
     */
    @DeleteMapping("/attraction/{id}")
    public ResultVO<Boolean> deleteAttraction(@PathVariable Long id) {
        return ResultVO.success(attractionService.removeById(id));
    }
}